package com.party.admin.web.controller.mall;

import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.core.model.mall.MallDataCountWithMember;
import com.party.core.service.mall.IMallDataCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 商城数据控制器
 * @Author: yangshoukun
 * @Date: 2018/5/22 16:00
 */
@Controller
@RequestMapping(value = "/mall/data")
public class MallDataController {

    @Autowired
    private IMallDataCountService mallDataCountService;

    /**
     *
     * @param commonInput
     * @param mallData
     * @param page
     * @return
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(CommonInput commonInput, MallDataCountWithMember mallData, Page page) throws ParseException {
        ModelAndView mv = new ModelAndView("mall/data/goodsData");
        page.setLimit(20);
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        final String pattern = "yyyy-MM-dd";

        if (null != params.get("startDate")) {
            commonInput.setCreateStart(params.get("startDate").toString().substring(0, pattern.length()));
            // 处理本月大于30天的情况, 将第一天置为2号
            Date now = new Date();
            Date d1 = DateUtils.addDay(now, -30);
            Date d2 = DateUtils.parse(params.get("startDate").toString(), pattern);
            if (d1.getTime() > d2.getTime()) {
                String d = DateUtils.formatDate(d1, pattern);
                params.put("startDate", d + " 00:00:00");
                commonInput.setCreateStart(d);
            }
        }
        if (null != params.get("endDate")) {
            commonInput.setCreateEnd(params.get("endDate").toString().substring(0, pattern.length()));
        }
        List<MallDataCountWithMember> list;
        if (params.size() < 1) {
            list = mallDataCountService.webListPage(mallData, null, page);
        } else {
            list = mallDataCountService.webListPageQuery(mallData, params, page);
        }
        // 获取统计数
        Map<String, Object> statistics = mallDataCountService.getStatistics();
        mv.addObject("statistics", statistics);
        mv.addObject("list", list);
        mv.addObject("page", page);
        mv.addObject("commonInput", commonInput);
        mv.addObject("mallData", mallData);
        return mv;
    }
}
